
<template>
  <div>
    <h3 class="login-title">研究生招生管理系统</h3>
    <!-- model=数据绑定对象 -->
    <!-- ref=表单引用对象 -->
    <el-form ref="loginRef" :model="form" :rules="loginrules" class="login">
      <el-form-item label="账号" prop="username">
        <el-input
          type="text"
          placeholder="请输入用户名"
          v-model="form.username"
          prefix-icon="el-icon-user"
        ></el-input>
        <!-- v-model作用会随你的输入而改变，达到数据双向绑定 -->
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          placeholder="请输入密码"
          v-model="form.password"
          prefix-icon="el-icon-lock"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div class="login-button">
        <el-button type="primary" style="width:26%" @click="login()"
          >登录</el-button>
          </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script >
export default {
  name: "login",
  data() {
    return {
      //这是表单的数据绑定对象
      form: {
        username: "",
        password: "",
      },
      //对账户和密码加限制
      loginrules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods:{
    login(){
      this.$refs.loginRef.validate(async valid => {
         if(valid){
          //await只能用在async中
          console.log(this.form);
          const {data:res} = await this.$http.post('/login',this.form);
          console.log(res);
           if(res.code !== 20000){
             this.$message.error('登录失败');
               return 
           }else{
             this.$message.success("登陆成功");
             this.$router.push('/mainhome')
           }
        }else{
          return
        }
      });
    }
  },
}
</script>

<style>
.login {
  width: 350px;
  margin: 20px auto;
  border: 1px solid #dcdfe6;
  padding: 50px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  margin-top: 50px;
  text-align: center;
}

.login-button{
  text-align: center;
}
</style>